#include"BlockQueue.hpp"
#include<stdlib.h>
#include<time.h>
//阻塞队列实现生成消费者模型

void *Consumer(void*args)
{
    BlockQueue<int>* bl=static_cast<BlockQueue<int>*>(args);
    while(true)
    {
        int data;
        bl->Pop(data);
        //sleep(1);
        printf("Consumer, get a data : %d\n",data);
    }
}

void *Producter(void* args)
{
    BlockQueue<int>* bl=static_cast<BlockQueue<int>*>(args);
    while(true)
    {
        srand(time(NULL));
        int data=rand()%10;

        bl->Enque(data);
        //sleep(1);

        printf("Procducter data is : %d\n",data);
        
    }
}

int main()
{
    BlockQueue<int>*bl =new BlockQueue<int>(10);

    pthread_t t1,t2,t3,t4;

    pthread_create(&t1,nullptr,Producter,bl);
    pthread_create(&t2,nullptr,Producter,bl);
    pthread_create(&t3,nullptr,Consumer,bl);
    pthread_create(&t4,nullptr,Consumer,bl);

    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);
    pthread_join(t4,nullptr);

    delete bl;
    return 0;
}